using UnityEngine;
using Cinemachine;
using System.Collections;

//相机震动
public class CameraShake : MonoBehaviour
{
    public static CameraShake  Instance;
    CinemachineVirtualCamera virtualCamera;
    CinemachineBasicMultiChannelPerlin noise;
    public float shakeDuration = 0.5f;  // 震动持续时间
    private bool shaking = false;  // 是否正在震动

    public float m_AmplitudeGain = 1;//震动幅度
    public float m_FrequencyGain = 1;//震动频率

    private void Awake() {
        Instance = this;
    }

    void Start()
    {
        virtualCamera = GetComponent<CinemachineVirtualCamera>();
        noise = virtualCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
    }

    // 发起震动
    public void PlayCameraShake()
    {
        StartCoroutine(Shake());
    }

    IEnumerator Shake()
    {
        if (shaking == false)
        {
            shaking = true;
            noise.m_AmplitudeGain = m_AmplitudeGain;
            noise.m_FrequencyGain = m_FrequencyGain;
            yield return new WaitForSeconds(shakeDuration);
            noise.m_AmplitudeGain = 0;
            noise.m_FrequencyGain = 0;
            shaking = false;
        }
    }
}
